<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

<fmt:setBundle basename="messages"/>

<script type="text/javascript">
	function submitSaveNewVersion(form){
		if (validateNewVersionForm()){
			form.action = "saveNewVersion.do";
			
			form.submit();
		}
	}

	function submitCancel(form){
		if (validSubmit){
			validSubmit = false;
			form.action = "versionList.do";

			form.submit();
		}
	}

	function validateNewVersionForm(){
		var validForm = false;
		var validDates = false;
		var versionStartDate = document.getElementById('versionStartDate').value;
		var versionEndDate = document.getElementById('versionEndDate').value;
		var versionStartDateTokens = versionStartDate.split("|");
		var versionEndDateTokens = versionEndDate.split("|");
		var versionStartDateMonth = "";
		var versionStartDateYear = "";
		var versionEndDateMonth = "";
		var versionEndDateYear = "";
		var versionCode = document.getElementById('versionCode').value;
		var versionDescription = document.getElementById('versionDescription').value;

		versionCode = trim(versionCode);
		versionDescription = trim(versionDescription);
		
		if (versionEndDate != -1){
			versionStartDateMonth = versionStartDateTokens[0];
			versionStartDateYear = versionStartDateTokens[1];
			versionEndDateMonth = versionEndDateTokens[0];
			versionEndDateYear = versionEndDateTokens[1];
						
			if (parseInt(versionEndDateYear) < parseInt(versionStartDateYear)){
				alert('<fmt:message key="warning.message.version.incorrect.range"/>');
			} else if ((parseInt(versionEndDateYear) == parseInt(versionStartDateYear)) && (parseInt(versionEndDateMonth) < parseInt(versionStartDateMonth))) {
				alert('<fmt:message key="warning.message.version.incorrect.range"/>');
			} else {
				validDates = true;
			}
		} else {
			alert('<fmt:message key="warning.message.version.required.finalperiod"/>');
		}
		 
		if (validDates){
			if (versionCode.length == 0){
				alert('<fmt:message key="warning.message.version.required.code"/>');
			} else if(versionDescription.length == 0){
				alert('<fmt:message key="warning.message.version.required.description"/>');
			} else {
				document.getElementById('startDate').value = versionStartDateTokens[2];
				document.getElementById('endDate').value = versionEndDateTokens[2];
				validForm = true;
			}
		}

		return validForm;
	}
</script>

<form name="NewVersionForm" method="POST">
	<table width="100%" align="center" border="0">
		<tr>
			<td style="width: 100%" align="center">
				<table style="width: 340" align="center" border="0" cellspacing="1" bgcolor="#CCCCC0">
					<tr>
						<th colspan="2" align="center" class="titulocolumna">
							<fmt:message key="label.versions.newVersion"/>
						</th>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.startDate"/>
						</td>
						<td class="etiqueta_campo">
							<input type="hidden" name="versionView.startDate" id="startDate"/>
							<select id="versionStartDate" class="campo_texto">
								<c:forEach items="${monthPeriods}" var="monthPeriod" varStatus="status">
									<c:choose>
										<c:when test="${monthPeriod.monthPeriodId == actualMonth}">
											<option value="<c:out value="${monthPeriod.month}"/>|<c:out value="${monthPeriod.year}"/>|<c:out value="${monthPeriod.listDescription}"/>" selected="selected"><c:out value="${monthPeriod.listDescription}"/></option>
										</c:when>
										<c:otherwise>
											<option value="<c:out value="${monthPeriod.month}"/>|<c:out value="${monthPeriod.year}"/>|<c:out value="${monthPeriod.listDescription}"/>"><c:out value="${monthPeriod.listDescription}"/></option>
										</c:otherwise>
									</c:choose>
								</c:forEach>
							</select>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.endDate"/>
						</td>
						<td class="etiqueta_campo">
							<input type="hidden" name="versionView.endDate" id="endDate"/>
							<select id="versionEndDate" class="campo_texto">
								<option value="-1" selected="selected"><fmt:message key="label.program.purchase.selectanoption"/></option>
								<c:forEach items="${monthPeriods}" var="monthPeriod" varStatus="status">
									<option value="<c:out value="${monthPeriod.month}"/>|<c:out value="${monthPeriod.year}"/>|<c:out value="${monthPeriod.listDescription}"/>"><c:out value="${monthPeriod.listDescription}"/></option>
								</c:forEach>
							</select>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.code"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="versionView.shortName" id="versionCode" class="campo_texto" size="15" maxlength="5"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.description"/>
						</td>
						<td class="etiqueta_campo">
							<input type="text" name="versionView.description" id="versionDescription" class="campo_texto" size="15" maxlength="100"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.importFrom"/>
						</td>
						<td class="etiqueta_campo">
							<select name="versionView.parentId" class="campo_texto">
								<option value="-1" selected="selected"><fmt:message key="label.program.purchase.selectanoption"/></option>
								<c:forEach items="${versions}" var="version" varStatus="status">
									<c:if test="${version.active}">
										<option value="<c:out value="${version.versionId}"/>"><c:out value="${version.shortName}"/>&nbsp;(<c:out value="${version.startDate}"/> - <c:out value="${version.endDate}"/>)</option>
									</c:if>
								</c:forEach>
							</select>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							&nbsp;
						</td>
						<td class="etiqueta_campo">
							<fmt:message key="label.versions.copy"/><input type="radio""" name="versionView.relatedType" id="versionRelatedType" checked="checked"  value="C"/>
							<fmt:message key="label.versions.initial.values"/><input type="radio""" name="versionView.relatedType" id="versionRelatedType"  value="I"/>
						</td>
					</tr>
					<tr>
						<td class="etiqueta" style="width: 35%">
							<fmt:message key="label.versions.active"/>
						</td>
						<td class="etiqueta_campo">
							<input type="checkbox" name="versionView.active" id="versionActive" checked="checked" disabled="disabled"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td height="10">
			</td>
		</tr>
		<tr>
			<td width="100%">
				<table align="center" width="340" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
					<tr>
						<td style="text-align: center; vertical-align: bottom;" class="columnadatoPar">
							<input type="button" value="<fmt:message key="label.common.save"/>" onclick="submitSaveNewVersion(NewVersionForm);" class="boton"/>
							<input type="button" value="<fmt:message key="label.common.cancel"/>" onclick="submitCancel(NewVersionForm);" class="boton"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>